CKEDITOR.plugins.add('saveWithApplet',{
  
  init: function(editor){
    editor.ui.addButton('openWithApplet',
      {
        label: 'Open',
        command: 'openWithApplet',
        icon: CKEDITOR.plugins.getPath('saveWithApplet') + 'images/openWithApplet.png'
      });
    
    editor.addCommand( 'openWithApplet',
      {
        modes : { wysiwyg:1, source:1 },
    		canUndo : false,
    		exec : function( editor ){
    		  if(CKEDITOR.pdfApplet){
            var contents = CKEDITOR.pdfApplet.open();
            editor.insertHtml(contents);
          }  
    		}
    	});
    
    editor.ui.addButton('saveWithApplet',
      {
        label: 'Save',
        command: 'saveWithApplet',
        icon: CKEDITOR.plugins.getPath('saveWithApplet') + 'images/saveWithApplet.png'
      });
      
     editor.addCommand( 'saveWithApplet',
      {
        modes : { wysiwyg:1, source:1 },
    		canUndo : false,
    		exec : function( editor )
    		{
    			var sHTML,
    				config = editor.config,
    				baseTag = config.baseHref ? '<base href="' + config.baseHref + '"/>' : '',
    				isCustomDomain = CKEDITOR.env.isCustomDomain();
    
    			if ( config.fullPage )
    			{
    				sHTML = editor.getData()
    						.replace( /<head>/, '$&' + baseTag )
    						.replace( /[^>]*(?=<\/title>)/, editor.lang.preview );
    			}
    			else
    			{
    				var bodyHtml = '<body ',
    						body = editor.document && editor.document.getBody();
    
    				if ( body )
    				{
    					if ( body.getAttribute( 'id' ) )
    						bodyHtml += 'id="' + body.getAttribute( 'id' ) + '" ';
    					if ( body.getAttribute( 'class' ) )
    						bodyHtml += 'class="' + body.getAttribute( 'class' ) + '" ';
    				}
    
    				bodyHtml += '>';
    
    				sHTML =
    					editor.config.docType +
    					'<html dir="' + editor.config.contentsLangDirection + '">' +
    					'<head>' +
    					baseTag +
    					'<title>' + editor.lang.preview + '</title>' +
    					CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
    					'</head>' + bodyHtml +
    					editor.getData() +
    					'</body></html>';
    			}
    
    			var iWidth	= 640,	// 800 * 0.8,
    				iHeight	= 420,	// 600 * 0.7,
    				iLeft	= 80;	// (800 - 0.8 * 800) /2 = 800 * 0.1.
    			try
    			{
    				var screen = window.screen;
    				iWidth = Math.round( screen.width * 0.8 );
    				iHeight = Math.round( screen.height * 0.7 );
    				iLeft = Math.round( screen.width * 0.1 );
    			}
    			catch ( e ){}
    			
    			if(CKEDITOR.pdfApplet){
            CKEDITOR.pdfApplet.save(sHTML);
          }
        }
      }); 
  }
});